<?php
namespace app\Services\admin;


use yii;
use app\models\Room;
use app\models\Roompic;
use app\models\Steaming;
use yii\db\Expression;
use app\Services\ApiService;


class RoomService extends ApiService
{

    /**
     * @return Service_Pay
     */
    public static function getInstance()
    {
        return parent::getInstance();
    }

    /**
     * 获取节目列表
     */
    public function getProgramList($req){
        $list = Room::find()
            ->alias('room')
            ->select('ID,room.name')
            ->asArray()
            ->all();
        return ['code'=>200,'data'=>$list];
    }

    /**
     * 获取节目详情
     */
    public function getProgramInfo($program_id){
        $room = Room::find()
            ->where(['ID'=>$program_id])
            ->select('ID,PicID,name,opentime,host,normal,reporter,VIP,Activity')
            ->asArray()
            ->one();
        if ($room['normal']==1){
            $room['type'] = 'normal';
        }elseif ($room['reporter']==1){
            $room['type'] = 'reporter';
        }elseif ($room['VIP']==1){
            $room['type'] = 'VIP';
        }
        $info = Steaming::find()
            ->select('steamURL')
            ->where(['roomID'=>$room['ID']])
            ->asArray()
            ->one();

        # 图片信息
        $room['pic_url'] =  '';
        if (!empty($room['PicID'])){
            $pic =  Roompic::find()
                ->where(['ID'=>intval($room['PicID'])])
                ->select('url')
                ->asArray()
                ->one();
            if (!empty($pic)){
                $room['pic_url'] =  $pic['url'];
            }
        }
        $room['steamURL'] = $info['steamURL'];
        return ['code'=>200,'data'=>$room];
    }


    /**
     * 保存房间设置
     */
    public function saveProgram($req){
        $room = Room::find()
            ->where(['ID'=>$req['ID']])
            ->asArray()
            ->one();
        if (empty($room)){
            return  ['code'=>-1,'message'=>'节目编号错误'];
        }
        $info['Activity'] = intval($req['Activity']);
        $info['host'] = intval($req['host']);
        if (!empty($req['opentime'])){
            $info['opentime'] = $req['opentime'];
        }
        switch ($req['type'])
        {
            case "normal":
                $info['normal'] = 1;
                $info['reporter'] = 0;
                $info['VIP'] = 0;
            break;
            case "reporter":
                $info['reporter'] = 1;
                $info['normal'] = 0;
                $info['VIP'] = 0;
            break;
            case "VIP":
                $info['normal'] = 0;
                $info['reporter'] = 0;
                $info['VIP'] = 1;
            break;
        }
        # 封面图
        if (!empty($req['PicID'])){
            $info['PicID'] = $req['PicID'];
        }
        Yii::$app->db->createCommand()->update('`pltf.room`' ,$info, ['ID'=> intval($req['ID'])])->execute();
        # 节目流地址
        if (!empty($req['steamURL'])){
            $st = Steaming::find()
                ->where(['roomID'=>$req['ID']])
                ->asArray()
                ->one();
            if (!empty($st)){
                Yii::$app->db->createCommand()->update('`pltf.steaming`' ,['steamURL'=>$req['steamURL']], ['roomID'=> intval($req['ID'])])->execute();
            }else{
                $insert = [
                    'steamURL'=>$req['steamURL'],
                    'roomID'  =>$req['ID']
                ];
                Yii::$app->db->createCommand()->insert('`pltf.steaming`' ,$insert)->execute();
            }
        }
        return  ['code'=>200,'message'=>'保存成功'];
    }

    /**
     * 增加房间
     */
    public function addProgram($name){
        $insert = [
            'name'=>$name,
        ];
        $res = Yii::$app->db->createCommand()->insert('`pltf.room`' ,$insert )->execute();
        $insert_id = Yii::$app->db->getLastInsertId();
        # 房间座位初始化
        $seat_arr = [
            [14,1,'0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [14,2,'0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [16,3,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [19,4,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [19,5,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,6,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,7,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,8,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,9,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,10,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,11,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,12,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
            [21,13,'0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0'],
        ];
        $time = date("Y-m-d H:i:s",time());
        foreach ($seat_arr AS $seat){
            $insert = [
                'roomID' => $insert_id,
                'maxrow' => $seat[0],
                'line'   => $seat[1],
                'seatdate'=> $seat[2],
                'updatetime'=> $time
            ];
            Yii::$app->db->createCommand()->insert('`pltf.seat`' ,$insert )->execute();
        }

        if ($res){
            return  ['code'=>200,'message'=>'创建成功'];
        }
        return  ['code'=>-1,'message'=>'创建失败'];
    }


    /**
     * 上传文件
     */
    public function uploadFile($file_info){
        unset($file_info['r']);
        $rs = Yii::$app->db->createCommand()->insert('`pltf.roompic`', $file_info)->execute();
        if ($rs){
            $file_info['file_id'] =  Yii::$app->db->getLastInsertID();
            return ['code'=>200,'message'=>'上传成功','data'=>$file_info];
        }
        return ['code'=>-1,'message'=>'上传失败'];
    }

}